﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using SYcwpb.Helpers;

namespace SYcwpb.Views
{
    /// <summary>
    /// SetChiefDriversTime.xaml 的交互逻辑
    /// </summary>
    public partial class SetChiefDriversTime : Window
    {
        private SqlDataLayer sqlHelper = new SqlDataLayer();
        private int line = Convert.ToInt32(CONSTDATA.UserModel.Line);
        public static SetChiefDriversTime _view = null;
        public List<ClsModel> list;

        public static SetChiefDriversTime GetInstance()
        {
            if (null == _view || WindowHelper.IsDisposed(_view))
                _view = new SetChiefDriversTime();
            return _view;
        }

        private SetChiefDriversTime()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            list = InitDriverComboBox();
            cbbcls.ItemsSource = list;
            cbbcls.SelectedValuePath = "cls_id";
            cbbcls.DisplayMemberPath = "cls_name";
            cbbcls.SelectedIndex = 0;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string cls = cbbcls.SelectedValue.ToString();
            string SJZInTimeStr = SJZInTime.Text;
            string SJZOutTimeStr = SJZOutTime.Text;
            string PBYInTimeStr = PBYInTime.Text;
            string PBYOutTimeStr = PBYOutTime.Text;
            string DDYInTimeStr = DDYInTime.Text;
            string DDYOutTimeStr = DDYOutTime.Text;

            DateTime time1 = new DateTime();
            DateTime time2 = new DateTime();

            if (!DateTime.TryParse(SJZInTimeStr, out time1))
            {
                MessageBox.Show("司机长出勤时间必须为时间格式");
                return;
            }
            if (!DateTime.TryParse(SJZOutTimeStr, out time2))
            {
                MessageBox.Show("司机长退勤时间必须为时间格式");
                return;
            }
            if (time1 > time2)
            {
                MessageBox.Show("司机长退勤时间必须大于出勤时间");
                return;
            }

            if (!DateTime.TryParse(PBYInTimeStr, out time1))
            {
                MessageBox.Show("排班员出勤时间必须为时间格式");
                return;
            }
            if (!DateTime.TryParse(PBYOutTimeStr, out time2))
            {
                MessageBox.Show("排班员退勤时间必须为时间格式");
                return;
            }
            if (time1 > time2)
            {
                MessageBox.Show("排班员退勤时间必须大于出勤时间");
                return;
            }

            if (!DateTime.TryParse(DDYInTimeStr, out time1))
            {
                MessageBox.Show("督导员出勤时间必须为时间格式");
                return;
            }
            if (!DateTime.TryParse(DDYOutTimeStr, out time2))
            {
                MessageBox.Show("督导员退勤时间必须为时间格式");
                return;
            }
            if (time1 > time2)
            {
                MessageBox.Show("督导员退勤时间必须大于出勤时间");
                return;
            }

            string sql1 = "update SetChiefDriversTime set InTime='" + SJZInTimeStr + "' , OutTime='" + SJZOutTimeStr + "' where [LineID]='" + line + "' and [Cls_id]='" + cls + "' and PositionID='14'";
            sqlHelper.ExecuteSql(sql1);
            string sql2 = "update SetChiefDriversTime set InTime='" + PBYInTimeStr + "' , OutTime='" + PBYOutTimeStr + "' where [LineID]='" + line + "' and [Cls_id]='" + cls + "' and PositionID='11'";
            sqlHelper.ExecuteSql(sql2);
            string sql3 = "update SetChiefDriversTime set InTime='" + DDYInTimeStr + "', OutTime='" + DDYOutTimeStr + "' where [LineID]='" + line + "' and [Cls_id]='" + cls + "' and PositionID='33'";
            sqlHelper.ExecuteSql(sql3);
            MessageBox.Show("更新成功");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            cbbcls.SelectedIndex = 0;
            this.Close();
        }

        private void cbbcls_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string cls = cbbcls.SelectedValue.ToString();
            string strSql = "select [PositionID],[Cls_id],[InTime],[OutTime] from [SetChiefDriversTime] where [LineID]='" + line + "' and [Cls_id]='" + cls + "'";
            DataTable DT = sqlHelper.SelectDt(strSql);
            foreach (DataRow row in DT.Rows)
            {
                if (row["PositionID"].ToString() == "14")
                {
                    SJZInTime.Text = row["InTime"].ToString();
                    SJZOutTime.Text = row["OutTime"].ToString();
                }
                if (row["PositionID"].ToString() == "11")
                {
                    PBYInTime.Text = row["InTime"].ToString();
                    PBYOutTime.Text = row["OutTime"].ToString();
                }
                if (row["PositionID"].ToString() == "33")
                {
                    DDYInTime.Text = row["InTime"].ToString();
                    DDYOutTime.Text = row["OutTime"].ToString();
                }
            }
        }

        public List<ClsModel> InitDriverComboBox()
        {
            List<ClsModel> lstcls = new List<ClsModel>();

            string strSql = "select [cls_id],[cls_name] from [ClassType] where cls_id<>'1' order by cls_id";
            DataTable DT = sqlHelper.SelectDt(strSql);
            if (DT.Rows.Count > 0)
            {
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    ClsModel _cls = new ClsModel();
                    _cls.cls_id = int.Parse(DT.Rows[i]["cls_id"].ToString());
                    _cls.cls_name = DT.Rows[i]["cls_name"].ToString();
                    lstcls.Add(_cls);
                }
            }
            return lstcls;
        }
    }

    public class ClsModel
    {
        public int cls_id {get;set;}
        public string cls_name {get;set;}
    }
}
